﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

namespace ZK.Common
{
  
    /// <summary>
    /// Cache辅助类
    /// </summary>
    public class CacheHelper
    {
        public static MemoryCache memoryCache = MemoryCache.Default;
        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="CacheKey">键</param>
        public static object GetCache(string CacheKey)
        {
            return memoryCache["CacheKey"];
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public static void SetCache(string CacheKey, object objObject)
        {
            memoryCache.Set(CacheKey, objObject,DateTimeOffset.MaxValue);
        }

        /// <summary>
        ///  设置数据缓存
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="time">到期时间</param>
        public static void SetCache(string CacheKey, object objObject, DateTime time)
        {
            memoryCache.Set(CacheKey, objObject, time);
        }

       

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        public static void RemoveOne(string CacheKey)
        {
            memoryCache.Remove(CacheKey);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            foreach (var item in memoryCache)
            {
                memoryCache.Remove(item.Key);
            }
        }
    }
}
